package com.example.software.controller;

import java.util.*;

// import org.apache.shiro.authz.annotation.RequiresPermissions;


import com.example.software.entity.DepartmentEntity;
import com.example.software.entity.Result;
import com.example.software.entity.WorkdayEntity;
import com.example.software.result.PageResult;
import com.example.software.service.*;
import com.example.software.vo.DepartmentVO;
import com.example.software.vo.RecodeVO;
import com.example.software.vo.workdayVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.software.entity.RecodeEntity;
import com.example.software.utils.PageUtils;
import com.example.software.utils.R;



/**
 * 
 *
 * @author wxy
 * @email wxy9496099@163.com
 * @date 2024-10-28 10:54:36
 */
@RestController
@Slf4j
@Api(tags = "挂号预约相关接口")
@RequestMapping("software/recode")
public class RecodeController {
    @Autowired
    private RecodeService recodeService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private WorkdayService workdayService;

    @Autowired
    private UserService userService;

    @Autowired
    private DepartmentService departmentService;


    //用websocket实现有新的预约信息的实时通知


    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询挂号预约信息(完整版)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "每页数量",dataType = "int"),
            @ApiImplicitParam(name = "doctorId", value = "医生id，如果查全部可以不传或者传空"),
            @ApiImplicitParam(name = "state", value = "状态，1:成功，0:取消，2:成功且已支付，3:爽约，4已核销，5已就诊.查全部可以不传或者传6"),
            @ApiImplicitParam(name = "name", value = "病人姓名模糊查询，如果查全部可以不传或者传'全部'")
    })
   // @RequiresPermissions("software:recode:list")
    public Result<PageUtils> list(@RequestParam String page,
                                  @RequestParam String size,
                                    @RequestParam(required = false) Long doctorId,
                                  @RequestParam(required = false) Integer state,
                                    @RequestParam(required = false) String name){

//医生叫号，根据医生id进行查询已核销的挂号记录，并从小到大排序，并且更新状态为已叫号，并且更新就诊时间

        Map<String, Object> params = new HashMap<>();
        params.put("page",page);
        params.put("limit",size);

        if (doctorId!=null && doctorId!= 0 ){
            params.put("doctorId",doctorId);
        }

        if (state!=null && state!= 6){
            params.put("state",state);
        }

        if (name!=null && !name.equals("0")){
            params.put("name",name);
        }

//        PageUtils page2 = recodeService.queryPage(params);

        System.out.println("name"+ name);
        PageUtils page2 = recodeService.queryPage2(page, size, doctorId, state, name);

        System.out.println("page"+ page2.getList());

        List<?> list = page2.getList();

        List<RecodeVO> list2 = new ArrayList<>();

        for (Object o : list) {
            RecodeEntity recodeEntity = (RecodeEntity) o;

            RecodeVO recodeVO = new RecodeVO();
            BeanUtils.copyProperties(recodeEntity,recodeVO);

            recodeVO.setDoctor(doctorService.getById(recodeEntity.getDoctorId()));
            recodeVO.setUser(userService.getById(recodeEntity.getUserId()));
            WorkdayEntity workdayEntity = workdayService.getById(recodeEntity.getWorkdayId());
            workdayVO workdayVO = new workdayVO();
            BeanUtils.copyProperties(workdayEntity,workdayVO);
            workdayVO.setDepartment(departmentService.getById(workdayEntity.getDepartmentId()));
            recodeVO.setWorkday(workdayVO);

            list2.add(recodeVO);
        }

        System.out.println(list2);
        list2.sort(Comparator.comparingInt(RecodeVO::getSerialNumber));

        page2.setList(list2);

        return Result.success(page2);
    }




//    @GetMapping("/list2")
//    @ApiOperation(value = "查询挂号预约信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "page", value = "页数", required = true),
//            @ApiImplicitParam(name = "size", value = "每页数量",dataType = "int"),
//            @ApiImplicitParam(name = "doctorId", value = "医生id，如果查全部可以不传或者传0"),
//    })
//    // @RequiresPermissions("software:recode:list")
//    public Result<PageUtils> list2(@RequestParam String page,
//                                  @RequestParam String size,
//                                  @RequestParam(required = false) Long doctorId){
//
//        Map<String, Object> params = new HashMap<>();
//        params.put("page",page);
//        params.put("limit",size);
//
//        if (doctorId!=null && doctorId!= 0 ){
//            params.put("doctorId",doctorId);
//        }
//
//        PageUtils page2 = recodeService.queryPage(params);
//
//        return Result.success(page2);
//    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据id查询预约详情")
    // @RequiresPermissions("software:recode:info")
    public Result<RecodeVO> info(@PathVariable("id") Long id){

		RecodeEntity recode = recodeService.getById(id);

        RecodeVO recodeVO = new RecodeVO();
        BeanUtils.copyProperties(recode,recodeVO);

        recodeVO.setDoctor(doctorService.getById(recode.getDoctorId()));
        recodeVO.setUser(userService.getById(recode.getUserId()));

        WorkdayEntity workdayEntity = workdayService.getById(recode.getWorkdayId());
        workdayVO workdayVO = new workdayVO();
        BeanUtils.copyProperties(workdayEntity,workdayVO);
        workdayVO.setDepartment(departmentService.getById(workdayEntity.getDepartmentId()));
        recodeVO.setWorkday(workdayVO);


        return Result.success(recodeVO);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation(value = "新增预约信息")
    // @RequiresPermissions("software:recode:save")
    public R save(@RequestBody RecodeEntity recode){
		recodeService.save(recode);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改预约信息")
    // @RequiresPermissions("software:recode:update")
    public R update(@RequestBody RecodeEntity recode){

		recodeService.updateById(recode);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation(value = "批量删除预约信息")
    // @RequiresPermissions("software:recode:delete")
    public R delete(@RequestBody Long[] ids){
		recodeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
